package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1 根据name模糊查询分页查询套餐信息，从setmeal表中

        Page<Setmeal> page = new Page<>();

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        //进行查询
        page = setmealMapper.selectPage(page, wrapper);

        //遍历
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据套餐categoryId区category表中查询对应的信息
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                //根据套餐的id去中间表查询当前套餐对应菜品信息
//                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
//                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
//                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
//                setmeal.setSetmealDishes(setmealDishList);

            }
        }
        return page;

    }

    @Override
    public void save(Setmeal setmeal) {
        //1 将基本信息保存到setmeal表
        setmealMapper.insert(setmeal);

        //2 遍历前端传入的菜品信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //设置上套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //将套餐菜品保存到setmeal_dish表
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //删除套餐 首先就要去判断当前要删除的这一批套餐是否有在售状态，一旦有一个在售，整个删除就会失效
    //删除套餐的时候，将要中间表的当前套餐和相关数据一并删除
    @Override
    public void deleteByIds(List<String> ids) {
        //删除套餐 首先就要去判断当前要删除的这一批套餐是否有在售状态，一旦有一个在售，整个删除就会失效
        //select count(0) from setmeal where status = 1 and in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus, 1)
                .in(Setmeal::getId, ids);

        Integer num = setmealMapper.selectCount(wrapper);
        if (num > 0) {
            throw new CustomException("当权套餐处于在售状态，无法删除");
        }

        //删除套餐的时候，将要中间表的当前套餐和相关数据一并删除
        //delete from setmeal where status = 0 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getStatus, 0)
                .in(Setmeal::getId, ids);
        setmealMapper.delete(wrapper1);

    }

    @Override
    public void updateById(Integer status, List<String> ids) {
        for (String id : ids) {
            LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Setmeal::getId, id);
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmealMapper.update(setmeal, wrapper);
        }
    }

    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);

        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());
        //根据套餐的id去中间表查询当前套餐对应菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;
    }

    @Override
    public void update(Setmeal setmeal) {
        //根据id直接先更新套餐的基本信息
        setmealMapper.updateById(setmeal);

        //删除菜品表中当前套餐的相关信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //将修改之后的菜品重新保存到
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());//设置套餐id
                setmealDishMapper.insert(setmealDish);
            }
        }

    }

    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);

        return setmealMapper.selectList(wrapper);

    }

    @Override
    public List<SetmealDish> findBySetmealId(Long setealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setealId);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(Dish::getId,setmealDish.getDishId());
                Dish dish = dishMapper.selectOne(wrapper1);
                setmealDish.setImage(dish.getImage());
            }
        }

        return setmealDishList;
    }

}
